public class SwapPairs {
    //迭代
    public ListNode swapPairs1(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode newHead = new ListNode(0);
        ListNode cur = head;
        ListNode curNext = head.next;
        ListNode prev = newHead;
        while(cur!=null&&curNext!=null){
            cur.next = curNext.next;
            prev.next = curNext;
            curNext.next = cur;
            prev = prev.next.next;
            cur = cur.next;
            if(cur!=null){
                curNext = cur.next;
            }else{
                curNext = null;
            }
        }
        return newHead.next;
    }
    //递归
    public ListNode swapPairs2(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode newHead = head.next;
        head.next = swapPairs2(newHead.next);
        newHead.next = head;
        return newHead;
    }
}
